﻿using System;

using HsTec.Mobile.Green.BLL;
using HsTec.Mobile.Green.Model;

public partial class PowerWarning_GoalOrientedEdit : System.Web.UI.Page
{
    private string sEditStatus = "1";
    private GoalOrienteBLL bll = new GoalOrienteBLL();
    private string sID = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_Pers_ID"] == null)
        {
            Response.Redirect("../Logon.aspx");
        }
        try
        {
            sID = this.Request.QueryString["ID"].ToString().Trim();
        }
        catch
        { }
        if (!IsPostBack)
        {
            try
            {
                sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();

                if (sEditStatus == "2")
                {
                    GoalOriente model = bll.GetModel(int.Parse(sID));
                    if (model != null)
                    {
                        txtConsumptionAccounting.Text = model.ConsumptionAccounting.ToString();
                        txtEnergyAccounting.Text = model.EnergyAccounting.ToString();
                        txtSetFrequency.Text = model.SetFrequency.ToString();
                        txtTelecommunicationTraffic.Text = model.TelecommunicationTraffic.ToString();
                        txtTrafficForecast.Text = model.TrafficForecast.ToString();
                        sltYear.Value = model.Years;
                    }
                }
            }
            catch
            {

            }
        }
    }
    protected void SaveData_ServerClick(object sender, EventArgs e)
    {
        GoalOriente model = new GoalOriente();
        try
        {
            model.ConsumptionAccounting = Decimal.Parse(txtConsumptionAccounting.Text.ToString().Trim());
            model.EnergyAccounting = Decimal.Parse(txtEnergyAccounting.Text.ToString().Trim());
            model.Years = sltYear.Value;
            model.SetFrequency = Decimal.Parse(txtSetFrequency.Text.ToString().Trim());
            model.TrafficForecast = Decimal.Parse(txtTrafficForecast.Text.ToString().Trim());
            model.TelecommunicationTraffic = Decimal.Parse(txtTelecommunicationTraffic.Text.ToString().Trim());
            model.OperatUser = 0;
            model.Type = "1";
            sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
            model.Remark = String.Empty;
            if (sEditStatus == "1")
            {
                if (bll.Exists(" Years='" + model.Years + "' and Type='1' "))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + model.Years + "年数据已经存在！');</script>");
                    return;
                }
                model.OperatDate = DateTime.Now;
                model.AddDate = DateTime.Now;
                int iback = bll.Add(model);
                if (iback > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功!');window.close();</script>");
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败!');</script>");
                    return;
                }
            }
            else
            {
                model.GO_ID = this.Request.QueryString["ID"].ToString().Trim() == "" ? 0 : int.Parse(this.Request.QueryString["ID"].ToString().Trim());
                if (bll.Exists(" Years='" + model.Years + "' and GO_ID<>'" + model.GO_ID + "'"))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + model.Years + "年数据已经存在！');</script>");
                    return;
                }
                model.OperatDate = DateTime.Now;
                model.AddDate = DateTime.Now;
                int iback = bll.Update(model);
                if (iback > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');window.close();</script>");  //window.opener.location.reload();
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败!');</script>");
                    return;
                }
            }
        }
        catch
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('操作失败!');</script>");
            return;
        }
    }
}
